package org.ielts.repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.ielts.domain.base.Lesson;
import org.ielts.domain.log.OrderLessonLog;
import org.ielts.domain.report.LessonWithListenerCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.yifeng.dao.HibernateRepository;

@Repository
public class OrderLessonLogRepository extends HibernateRepository<OrderLessonLog, String> {

	public Collection<OrderLessonLog> findBySignCourseAndLesson(String signCourseId, String[] lessonIds) {
		DetachedCriteria criteria = createDetachedCriteria();
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).createAlias("signCourse", "signCourse")
				.createAlias("lesson", "lesson").add(Restrictions.eq("signCourse.id", signCourseId))
				.add(Restrictions.in("lesson.id", lessonIds));

		@SuppressWarnings("unchecked")
		List<OrderLessonLog> orderLessonLogs = hibernateTemplate.findByCriteria(criteria);

		return orderLessonLogs;
	}

	public Iterable<OrderLessonLog> findBySignCourse(String signCourseId) {
		DetachedCriteria criteria = createDetachedCriteria();
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).createAlias("signCourse", "signCourse")
				.add(Restrictions.eq("signCourse.id", signCourseId)).addOrder(Order.desc("beginTime"));

		@SuppressWarnings("unchecked")
		List<OrderLessonLog> orderLessonLogs = hibernateTemplate.findByCriteria(criteria);

		return orderLessonLogs;
	}

	public Iterable<OrderLessonLog> findOrderLessonLogByLesson(String id) {
		DetachedCriteria criteria = createDetachedCriteria();

		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).createAlias("lesson", "lesson")
				.add(Restrictions.eq("lesson.id", id)).addOrder(Order.desc("createTime"));

		@SuppressWarnings("unchecked")
		List<OrderLessonLog> orderLessonLogs = hibernateTemplate.findByCriteria(criteria);

		return orderLessonLogs;
	}

	public List<OrderLessonLog> findOrderLessonLogByLessonAndStudent(String lessonId, String studentId) {
		DetachedCriteria criteria = createDetachedCriteria();

		criteria.createAlias("lesson", "lesson").add(Restrictions.eq("lesson.id", lessonId))
				.createAlias("student", "student").add(Restrictions.eq("student.id", studentId))
				.addOrder(Order.desc("createTime"));

		@SuppressWarnings("unchecked")
		List<OrderLessonLog> orderLessonLogs = hibernateTemplate.findByCriteria(criteria);

		return orderLessonLogs;
	}

	public long countByLesson(String id) {
		DetachedCriteria criteria = createDetachedCriteria();
		criteria.createAlias("lesson", "lesson").add(Restrictions.eq("lesson.id", id));

		long count = countRowByCriteria(criteria);

		return count;
	}
}
